﻿using Deployment.Data.UI.Controls.Deploy;
using Deployment.Data.UI.Dialogs.Step;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Deployment.Data.UI.Controls.Step
{
    /// <summary>
    /// Interaction logic for EditableStepList.xaml
    /// </summary>
    public partial class EditableStepList : UserControl
    {
        public static readonly DependencyProperty SelectedStepProperty =
            DependencyProperty.Register("SelectedStep",
                typeof(Deployment.Data.Deploy.Steps.Step),
                typeof(EditableStepList), null);

        public static readonly DependencyProperty DeploymentProperty =
            DependencyProperty.Register("Deployment",
                typeof(Data.Deploy.DeploymentFile),
                typeof(EditableStepList));

        public static readonly DependencyProperty EnvironmentsProperty =
            DependencyProperty.Register("Environments",
                typeof(Data.Environment.EnvironmentsFile),
                typeof(EditableStepList));

        public Data.Environment.EnvironmentsFile Environments
        {
            get { return (Data.Environment.EnvironmentsFile)GetValue(EnvironmentsProperty); }
            set { SetValueDp(EnvironmentsProperty, value); }
        }

        public event DependencyPropertyChangedEventHandler PropertyChanged;
        private void SetValueDp(DependencyProperty property, object value)
        {
            SetValue(property, value);
            if (PropertyChanged != null)
                PropertyChanged(this, new DependencyPropertyChangedEventArgs());
        }

        public Deployment.Data.Deploy.Steps.Step SelectedStep
        {
            get { return (Deployment.Data.Deploy.Steps.Step)GetValue(SelectedStepProperty); }
            set { SetValueDp(SelectedStepProperty, value); }
        }

        public Data.Deploy.DeploymentFile Deployment
        {
            get { return (Data.Deploy.DeploymentFile)GetValue(DeploymentProperty); }
            set { SetValueDp(DeploymentProperty, value); }
        }

        public EditableStepList()
        {
            InitializeComponent();
        }

        private void StepList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = StepList.SelectedItem as Deployment.Data.Deploy.Steps.Step;
            SelectedStep = item;
        }

        private void RemoveStep_Click(object sender, RoutedEventArgs e)
        {
            DeployEditorControl parentControl = WPFHelpers.TryFindParent<DeployEditorControl>(this);
            ObservableSortedList<Deployment.Data.Deploy.Steps.Step> steps = DataContext as ObservableSortedList<Deployment.Data.Deploy.Steps.Step>;
            if (steps != null)
            {
                Deployment.Data.Deploy.Steps.Step step = SelectedStep;
                if (step != null)
                {
                    MessageBoxResult result = MessageBox.Show(Window.GetWindow(this),
                        string.Format("Are you sure you want to remove Step (sequence: {0}, Description: {1} from the deployment?", step.Sequence, step.Description),
                        "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                        steps.Remove(step);
                        if (parentControl != null)
                            parentControl.HasUnsavedChanges = true;
                    }
                }
            }
        }

        private void AddStep_Click(object sender, RoutedEventArgs e)
        {
            DeployEditorControl parentControl = WPFHelpers.TryFindParent<DeployEditorControl>(this);
            AddStepDialog dlg = new AddStepDialog();
            dlg.Owner = Window.GetWindow(this);
            dlg.Deployment = this.Deployment;
            dlg.Environments = this.Environments; 

            bool? result = dlg.ShowDialog();
            if (result.HasValue && result.Value == true)
            {
                Data.Deploy.Steps.Step newStep = dlg.NewStep;
                (this.DataContext as ObservableSortedList<Data.Deploy.Steps.Step>).Add(newStep);
                if (parentControl != null)
                    parentControl.HasUnsavedChanges = true;
            }
        }
    }
}
